package leetcode.weekly.week292;

import leetcode.helper.H;
import leetcode.helper.tree.TreeArray;
import leetcode.helper.tree.TreeNode;

//Solution2Test
public class Solution2 {

	int ans;

	public int averageOfSubtree(TreeNode root) {
		ans = 0;
		this.sum(root);// sum , cnt
		return ans;
	}

	private int[] sum(TreeNode root) {
		if (root == null) {
			return new int[] { 0, 0 };
		}
		int[] left = this.sum(root.left);
		int[] right = this.sum(root.right);
		int cur = root.val;
		int total = left[0] + right[0] + cur;
		int cnt = left[1] + right[1] + 1;
		if (cnt != 0) {
			int avg = total / cnt;
			if (avg == cur) {
				ans += 1;
			}
		}
		return new int[] { total, cnt };
	}

	public void debug4() {
		H.compare(5, this.averageOfSubtree(TreeArray.array2tree(H.str2array4("[4,8,5,0,1,null,6]"))));
	}

	public static void main(String[] args) {
		Solution2 so = new Solution2();
		so.debug4();
	}

}
